<?php
session_start();
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) 
{
	
	$con = mysql_connect("db.cs.dal.ca","tameem","B00614969");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("tameem", $con);
	
	if($_GET['action']=="a")
	{
		if($_GET[table]==1)
		{
			//temporary id increment
			
			$sql="SELECT MAX(id) FROM AdminCommittees";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			$id = $row['MAX(id)'];
			$id +=1;
			
			$sql="INSERT INTO AdminCommittees (id, name, committeeType, role, start, end)
			VALUES ('$id','$_POST[name]','$_POST[ct]','$_POST[role]','$_POST[sd]','$_POST[ed]')";
		}
		else if($_GET[table]==3)
		{
			//temporary id increment
			
			$sql="SELECT MAX(id) FROM EducationScholarships";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			$id = $row['MAX(id)'];
			$id +=1;
			
			$sql="INSERT INTO EducationScholarships (id, AwardingGroup, AwardName, start, end)
			VALUES ('$id','$_POST[ag]','$_POST[an]','$_POST[sd]','$_POST[ed]')";
		}
		else if($_GET[table]==4)
		{
			//temporary id increment
			
			$sql="SELECT MAX(id) FROM EmploymentHistory";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			$id = $row['MAX(id)'];
			$id +=1;
			
			$sql="INSERT INTO EmploymentHistory (id, jobTitle, groupName, company, start, end, superVisor, jobDescription)
			VALUES ('$id','$_POST[jt]','$_POST[gn]', '$_POST[comp]','$_POST[sd]','$_POST[ed]','$_POST[sv]','$_POST[jd]')";
		
		}
	}
	else if($_GET['action']=="e")
	{
		if($_GET[table]==1)
		{
			$sql="Update AdminCommittees SET name='$_POST[name]', committeeType='$_POST[ct]', role='$_POST[role]', start='$_POST[sd]', end='$_POST[end]'
			WHERE id='$_GET[id]'";	
		}
		else if($_GET[table]==3)
		{
			$sql="Update EducationScholarships SET AwardingGroup='$_POST[ag]', AwardName='$_POST[an]', start='$_POST[sd]', end='$_POST[ed]'
			WHERE id='$_GET[id]'";	
		}
		else if($_GET[table]==4)
		{
			$sql="Update EmploymentHistory SET jobTitle='$_POST[jt]', groupName='$_POST[gn]', company='$_POST[comp]', start='$_POST[sd]', end='$_POST[ed]' , superVisor='$_POST[sv]', jobDescription='$_POST[jd]'
			WHERE id='$_GET[id]'";	
		}
	}
	else if($_GET['action']=="d")
	{
		if($_GET[table]==1)
		{
			$sql="DELETE FROM AdminCommittees WHERE id='$_GET[id]'";
		}
		else if($_GET[table]==3)
		{
			$sql="DELETE FROM EducationScholarships WHERE id='$_GET[id]'";
		}
		else if($_GET[table]==4)
		{
			$sql="DELETE FROM EmploymentHistory WHERE id='$_GET[id]'";
		}
	}
		  
	if(!mysql_query($sql))
	{
		die('Error: ' . mysql_error());
	}
	mysql_close($con);
	header("location:data.php?table=".$_GET[table]);
}
else
{
	header("location:data.php");
}
?>